<?php

namespace App\Http\Controllers\Admin;

use Validator;
use Illuminate\Validation\Rule;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Repositories\AdminRoleRepository;
use App\Repositories\AdminMenuRepository;
use App\Models\AdminRole;

class RoleController extends Controller
{
    protected $Repo;
    protected $Menus;

    public function __construct(AdminRoleRepository $repo, AdminMenuRepository $menus)
    {
        $this->middleware('admin.auth');
        $this->Repo = $repo;
        $this->Menus = $menus;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $roles = $this->Repo->getRoles();
        return view('admin.roles.index', compact('roles'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.roles.createRole');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'title' => 'bail|required|unique:admin_role,title|max:50',
        ]);

        $result = $this->Repo->addRole($request);
        if (is_object($result)) {
            $data = [
                'status' => 0,
                'msg'    => '角色添加成功',
                'url'    => route('roles.index')
            ];
        } else {
            $data = [
                'status' => 2,
                'msg'    => '角色添加失败'
            ];
        }
        return json_encode($data);

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $role = AdminRole::find($id);
        return view('admin.roles.edit', compact('role'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        // $this->validate($request, [
        //     'title' => 'bail|required|unique:admin_role,title|max:50',
        //     'description' => 'nullable'
        // ]);

        $validator = Validator::make($request->all(), [
            'title' => ['bail','required', Rule::unique('admin_role')->ignore($id), 'max:50'],
        ],[
            'title.required' => '角色名称必填',
            'title.unique'   => '角色名称必须唯一',
        ])->validate();

        $this->Repo->updateRole($request, $id);
        $data = [
            'status' => 0,
            'msg'    => '角色更新成功',
            'url'    => route('roles.index')
        ];

        return json_encode($data);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $this->Repo->deleteRole($id);
        $data = [
            'status' => 0,
            'msg'    => '角色删除成功',
            'url'    => route('roles.index')
        ];
        return json_encode($data);
    }

    //批量删除
    public function destroyList(Request $request)
    {
        $ids = explode(',', $request->ids);

        if ($request->isMethod('post')) {
            $this->Repo->deleteRoleLists($ids);
            $data = [
                'status' => 0,
                'msg'    => '角色删除成功',
                'url'    => route('roles.index')
            ];
            return json_encode($data);
        }
    }

    //获取角色权限
    public function permissions($id)
    {
        $allMenus = $this->Menus->getAllMenus();
        $menus = $this->Repo->getRoleMenus($id);//角色已授权菜单

        return view('admin.roles.permission', compact('menus', 'allMenus', 'id'));
    }

    //保存权限分配
    public function updatePermissions(Request $request, $id)
    {
        $res = $this->Repo->updateRolePermissions($request, $id);
        if(!$res) {
            $data = ['status' => 2, 'msg' => '分配权限失败'];
        }else {
            $data = ['status' => 0, 'msg' => '分配权限成功'];
        }
        return json_encode($data);
    }
}
